<?php

namespace App\Console\Commands;

use Illuminate\Console\Command;
use DB;


class zhangweixi extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'zhang:test';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = 'Command description';

    /**
     * Create a new command instance.
     *
     * @return void
     */
    public function __construct()
    {
        parent::__construct();
    }

    /**
     * Execute the console command.
     *
     * @return mixed
     */
    public function handle()
    {

        self::check_ppt();
        //self::check_ppt_zip();
		
    }

    public static function check_ppt() {
        $ids = [150,169,195,199,240,242,250];
        $books = DB::table('bookinfo')
        //->whereIn('BookID',$ids)
        ->where('DeleteMark',1)
        ->where('IsShow',1)
        ->get();

        foreach($books as $book)
        {
            if(!$book->bg_img){
                continue;
            }
            
            $res = self::copy_file($book->bg_img);
            if($res == false) {
                echo $book->BookTitle.'-'.$book->bg_img."\n";
            }
            continue;
            $imgs = DB::table('bookimg')
            ->where('BookID', $book->BookID)
            ->orderBy('BookImgID')
            ->pluck('FilePath1');

            $ppts = scandir(public_path('ppt/'.$book->BookID));
            array_splice($ppts,0,2);
            $max = min(count($imgs),count($ppts));
            for($i=0;$i<$max;$i++) {

                $to = public_path(trim($imgs[$i],'/'));
                $from = public_path('ppt/'.$book->BookID.'/'.$ppts[$i]);
                copy($from,$to);
            }
        }
    }

    public static function check_ppt_zip() {
        $books = DB::table('bookinfo')->where('DeleteMark',1)->where('IsShow',1)->get();

        foreach($books as $book)
        {
            if(!$book->ppt){
                continue;
            }

            $res = self::copy_file($book->ppt);
            if(!$res) {
                echo $book->BookTitle.'-'.$book->ppt."\n";
            }
        }
    }

    public static function copy_file($file) {

        $oldImg = str_replace('uploads','uploads1',$file);
        $file = public_path(trim($file,'/'));
        if(file_exists($file)) {
            return true;
        }

        if(!file_exists($file) && file_exists($oldImg)) {

            $dir = dirname($file);
            if(!file_exists($dir))
            {
                mkdir($dir);
            }
            copy($oldImg,$file);
            return true;
        }

        return false;
    }
}
